package com.onlygx.dva.web.base.service;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * excel 服务
 * 导出到excel，并上传oss
 * @author : GaoXiang
 * 时间 : 2022/1/19
 * 介绍 :
 */
@Service
@RequiredArgsConstructor
public class ExcelService {

    final OSSService ossService;

    /**
     * 导出配置文件到oss
     */
    public String exportExcel(Class clazz, List list, String sName, String fName) {
        if (list.isEmpty()) {
            throw new NullPointerException("导出数据不能为空");
        }
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            EasyExcel.write(outputStream, clazz).sheet(sName).doWrite(list);
            String fileName = fName + IdUtil.simpleUUID();
            return ossService.pushFile(fileName + ".xlsx", new ByteArrayInputStream(outputStream.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }
}
